package com.yxe.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.yxe.mapper.ExpenseMapper;
import com.yxe.pojo.Expense;
import com.yxe.service.ExpenseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzq
 * @since 2022-12-09
 */
@Service
public class ExpenseServiceImpl extends ServiceImpl<ExpenseMapper, Expense> implements ExpenseService {

    @Resource
    private ExpenseMapper expenseMapper;


    @Override
    public Expense findByUserId(Integer id) {
        return expenseMapper.findByUserId(id);
    }

    @Override
    public void updateExpense(Integer payType, Float payMoney, Integer userId) {

        //获取数据库原数据
        Expense expense = expenseMapper.findByUserId(userId);

        System.out.println(expense);

        if (null == expense) {
            System.out.println("无数据");
            //设置新数据
            Expense expenseBill = new Expense();
            if (1 == payType) {
                expenseBill.setElectricityBill(payMoney);
                expenseBill.setWaterRate((float) 0);
                expenseBill.setGasBill((float) 0);
            }
            if (2 == payType) {
                expenseBill.setElectricityBill((float) 0);
                expenseBill.setWaterRate(payMoney);
                expenseBill.setGasBill((float) 0);
            }
            if (3 == payType) {
                expenseBill.setElectricityBill((float) 0);
                expenseBill.setWaterRate((float) 0);
                expenseBill.setGasBill(payMoney);
            }
            expenseBill.setUserId(userId);
            expenseMapper.insert(expenseBill);
        } else {
            //设置新数据
            if (1 == payType) {
                expense.setElectricityBill(expense.getElectricityBill() + payMoney);
            }
            if (2 == payType) {
                expense.setWaterRate(expense.getWaterRate() + payMoney);
            }
            if (3 == payType) {
                expense.setGasBill(expense.getGasBill() + payMoney);
            }
            //存入数据
            expenseMapper.updateExpense(expense);
        }
    }
}
